/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yorsh03.preespecializacion.cloudfoundry.crudjava.dao.impl;

import java.util.List;
import org.hibernate.Session;
import org.yorsh03.preespecializacion.cloudfoundry.crudjava.dao.IntegranteDao;
import org.yorsh03.preespecializacion.cloudfoundry.crudjava.model.Integrante;
import org.yorsh03.preespecializacion.cloudfoundry.crudjava.util.HibernateUtil;

/**
 *
 * @author jorge
 */
public class IntegranteDaoImpl implements IntegranteDao {

    @Override
    public void insertar(Integrante integrante) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(integrante);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al insertar: " +e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void actualizar(Integrante integrante) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.update(integrante);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al actualizar: " +e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void eliminar(Integrante integrante) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(integrante);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al eliminar: " +e.getMessage());
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Integrante buscarPorId(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        return (Integrante) session.load(Integrante.class, id);
    }

    @Override
    public List<Integrante> buscarTodos() {
         Session session = HibernateUtil.getSessionFactory().openSession();
         return session.createQuery("from Integrante").list();
        }
    
    
}
